package jp.co.cos.ittrick.channel.controller.client;

import jp.co.cos.ittrick.channel.model.PreModel;
import jp.co.cos.ittrick.channel.service.PreService;
import jp.co.cos.ittrick.channel.util.Utility;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import com.google.appengine.api.datastore.Text;

public class UpdatePreController extends Controller {

    private PreService preService;

    public UpdatePreController() {
        preService = new PreService();
    }

    @Override
    public Navigation run() throws Exception {

        // リクエストパラメータ
        String pre = asString("pre");
        String name = asString("name");
        String index = asString("index");
        String content = asString("content");

        // Modelの設定
        PreModel model = preService.getPreByKey(Integer.valueOf(pre));
        if(model != null) {
            model.setName(name);
            model.setIndex(Integer.valueOf(index));

            Utility util = new Utility();
            content = util.htmlEscape(content);
            model.setContent(new Text(content));

            // Insert
            preService.update(model);
        }

        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        response.getWriter().println("0");
        response.flushBuffer();

        return null;
    }
}
